package z.explorer.log;

import org.apache.log4j.*;
import org.apache.log4j.spi.*;
import javax.swing.*;
import org.apache.log4j.WriterAppender;

public class ZAppender extends AppenderSkeleton {

	JTextArea textArea = null;

	public ZAppender(){
		
	}
	public ZAppender(JTextArea area) {
		textArea = area;
		layout = new PatternLayout("%p [%t] %c  - %m%n");
	}

	public ZAppender(Layout pattern, JTextArea area) {
		layout = pattern;
		textArea = area;
	}

	public void setTextArea(JTextArea area) {
		textArea = area;
	}
	
	
	public static ZAppender getAppender(){
		Logger logger=Logger.getRootLogger();
		return (ZAppender)logger.getAppender("Z");
	}
	@Override
	protected void append(LoggingEvent arg0) {
		// TODO Auto-generated method stub
		if (textArea == null)
			return;
		else
			subAppend(arg0);
	}

	@Override
	public synchronized void close() {
		// TODO Auto-generated method stub
		if (closed)
			return;
		else
			closed = true;
	}

	@Override
	public boolean requiresLayout() {
		// TODO Auto-generated method stub
		return true;
	}

	public void subAppend(LoggingEvent event) {
		synchronized (textArea) {
			textArea.append(layout.format(event));
		}
	}

}
